//
//  LCNetMansagerTools.swift
//  SinaWeiBo
//
//  Created by mac on 16/12/3.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit
import AFNetworking

//定义一个枚举(网络请求的方式)
enum RequestType {
    case get
    case post
}

class LCNetMansagerTools: AFHTTPSessionManager {
    
    //MARK:给这个工具类设置成单利模式
    static var sharedTools:LCNetMansagerTools = {
       let tools = LCNetMansagerTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()

    
    //定义一个网络请求的方法
    func request(requestType: RequestType, url: String,parameters: Any?,callBack: @escaping (Any?,Error?)->()){
        //判断网络请求方式是get还是post
        if requestType == .get {
            get(url, parameters: parameters, progress: nil, success: {(_, responds) in
                //1get请求方式成功
                callBack(responds,nil)
                
            }, failure: {(_, error) in
                //get失败
                callBack(nil,error)
            })
        }else{
            post(url, parameters: parameters, progress: nil, success: {(_, responds) in
                //post请求成功
                callBack(responds,nil)
            }, failure: {(_, error) in
                //post请求失败
                callBack(nil,error)
            })
        }
    }
    

}
//拓展里边来向外暴露出各种网络请求的接口
extension LCNetMansagerTools{
    //1获取AsscessToKen的网络请求
    func requestAccessToKen(code: String,callBack: @escaping (Any?,Error?)->()){
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        
        let parameters:[String:Any] = [
            "client_id" : AppKey,
            "client_secret" : AppSecret,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : Redirect_url,
        ]
        request(requestType: .post, url: urlStr, parameters: parameters, callBack: callBack)
        
    }
    
    //2获取用户信息
    func requestUserInfo(access_token:String,uid:Int64, callBack: @escaping (Any?,Error?)->()){
        let urlStr = "https://api.weibo.com/2/users/show.json"
        
        let parameters: [String:Any] = [
            "access_token":access_token,
            "uid":uid
        ]
        
        request(requestType: .get, url: urlStr, parameters: parameters, callBack: callBack)
        
    }
    
    //3获取主页数据
    func requestHomeStatuses(maxId: Int64,sinceId:Int64 ,callBack: @escaping (Any?,Error?)->()){
        
        let urlStr = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let parameters: [String:Any] = [
        "access_token":LCOAuthViewModel.sharedViewModel.userAccount?.access_token ?? 0,
        "max_id":maxId,
        "since_id":sinceId
        ]
        //拼接途径
        let path = urlStr + "?access_token="+(LCOAuthViewModel.sharedViewModel.userAccount?.access_token)!
        print(path)
        
        request(requestType: .get, url: urlStr, parameters: parameters, callBack: callBack)
        
    }
    
    
    
}







